<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript"
	src="<%=path%>/script/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
		
		
		function sendXML(){
			var xmlStr = "<driverInfo>";
	xmlStr = xmlStr + "<driverClassName>com.sqlserver.driver.SQLServerDriver</driverClassName>";
	xmlStr = xmlStr + "<url>jdbc:sqlserver://127.0.0.1:xxx</url>";
	xmlStr = xmlStr + "<username>sa</username>";
	xmlStr = xmlStr + "<password>1</password>";
	xmlStr = xmlStr + "</driverInfo>";
	
		var data={
			"xmlStr":xmlStr
		};
		
		var sendURL="<%=path%>/servlet/xmlSevlet?date=" + new Date() + "";

		jQuery.post(sendURL, data, function(xmlData) {

			//从服务端接收到一个xml对象                 
			var userArray = xmlData.getElementsByTagName("userinfo");
			var optionHTML="";
			for (var i = 0; i < userArray.length; i++) {

					var userElement=userArray[i];

					var userid=userElement.getAttribute("userid");
					var username=userElement.getElementsByTagName("username")[0].firstChild.nodeValue;
					var age=userElement.getElementsByTagName("age")[0].firstChild.nodeValue;
					
					optionHTML=optionHTML+"<option value="+userid+">"+username+"="+age+"</option>";
					
					$("#userselect").html(optionHTML);   //改变id为userselect的标签的内容
			}

		}, "xml");

	}
</script>
</head>

<body>
	使用ajax来发送与接收XML的数据

	<input type="button" name="button1" id="button1" value="发送与接收XML"
		onclick="sendXML();" />

	<select id="userselect" style="width: 200px;"></select>
</body>
</html>
